//
//  HomeViewController.m
//  TingLiFeng
//
//  Created by xinxuan on 16/1/2.
//  Copyright © 2016年 xinxuan. All rights reserved.
//

#import "HomeViewController.h"
#import "MoreViewController.h"
#import "NoStarTableViewCell.h"
#import "AllViewController.h"
#import<CoreBluetooth/CoreBluetooth.h>
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,CBPeripheralManagerDelegate>

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *modelsArray;

@property (nonatomic,strong) CBPeripheralManager * manager;
@end


@implementation HomeViewController
static NSString *ID = @"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

    //不加这一句,第一次运行tableView的顶端会有大片空白
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"全部" highImageName:nil target:self action:@selector(more)];
    
    self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 40, Screen_Width, Screen_Height/5 * 4)];
    if (Screen_Width == 320 && Screen_Height == 480) {//苹果4
        
  self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 40, Screen_Width, Screen_Height/5 * 4)];
        
    }else if(Screen_Width == 320 && Screen_Height == 568){//苹果5
        
       
      self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 40, Screen_Width, Screen_Height/5 * 4)];
        
    }else if(Screen_Width == 375 && Screen_Height == 667){//苹果6
        
      self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 40, Screen_Width, Screen_Height/5 * 4 + 35) ];
        
        
    }else{//苹果6puls及其以上
        
      self.tableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 40, Screen_Width, Screen_Height/5 * 4 + 35) ];
        
    }
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;//隐藏水平滚动条
    [_tableView registerClass:[NoStarTableViewCell class] forCellReuseIdentifier:ID];
    
    [self.view addSubview:_tableView];
    
    [self addBottomLabel];
    
    [self creatModelsWithCount:4];
}


-(void)viewWillAppear:(BOOL)animated{

 [[NSNotificationCenter defaultCenter]postNotificationName:@"supportedInterfaceOrientations" object:self userInfo:@{@"surport" : [NSNumber numberWithBool:NO]}];

}


-(void)addBottomLabel{
    
    UIView * bottomLabel = [[UIView alloc]init];
    bottomLabel.frame = CGRectMake(0, 0, Screen_Width, Screen_Height / 5);
    bottomLabel.backgroundColor = [UIColor whiteColor];
    
    
    UIButton * moreButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    moreButton.layer.masksToBounds = YES;
    moreButton.layer.cornerRadius = 6;
    moreButton.frame = CGRectMake(Screen_Width /4, 20, Screen_Width/2 - 8, 32);
    [moreButton setBackgroundImage: [UIImage imageNamed:@"发现更多"] forState:(UIControlStateNormal)];
    
    
    UILabel * fontLabel = [[UILabel alloc]initWithFrame:CGRectMake(moreButton.frame.size.width/4, 9, moreButton.frame.size.width/2+ 10, 12)];
    fontLabel.text = @"发现更多课程";
    fontLabel.textAlignment = NSTextAlignmentCenter;
    fontLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    fontLabel.textColor = [UIColor colorFromHexString:@"#666666"];
    [moreButton addSubview:fontLabel];
    
    
    [moreButton addTarget:self action:@selector(more) forControlEvents:(UIControlEventTouchUpInside)];
    
    [bottomLabel addSubview:moreButton];
      
    _tableView.tableFooterView = bottomLabel;

}


-(void)more{
    
    AllViewController * allVC = [[AllViewController alloc]init];
    [self.navigationController pushViewController:allVC animated:YES];
    
}


- (void)creatModelsWithCount:(NSInteger)count
{
    if (!_modelsArray) {
        _modelsArray = [NSMutableArray new];
    }
    
    NSArray *picsArray = @[@"按摩课程1.jpg",
                           @"热敷教程2.jpg",
                                     @"理疗课程3.jpg",
                           
                                     @"音乐课程4.jpg"];
    
    NSArray * contentArray = @[@"激活胸腺细胞，是松散有游离脂肪聚焦",
                                     @"促进炎症消退，消除炎症肿胀和疼痛",
                                     @"刺激乳腺增长多重穴位刺激，享受SPA",
                                     @"起到镇定作用，消除失眠和神经紧张"];
    
    
    NSArray *namesArray = @[@"按摩课程",
                            @"热敷课程",
                            @"理疗课程",
                            @"音乐课程"];
    
    NSArray *periodArray = @[@"共3天",
                            @"共2天",
                            @"共4天",
                            @"共5天"];
    
    NSArray *personNumberArray = @[@"100人在练",
                            @"500人在练",
                            @"600人在练",
                            @"700人在练"];
    
    NSArray *timesArray = @[@"约10分钟/次",
                            @"约12分钟/次",
                            @"约8分钟/次",
                            @"约6分钟/次"];
    
    NSArray *levelsArray = @[@"初级",
                            @"高级",
                            @"初级",
                            @"高级"];
    
    
    for (int i = 0; i < 4; i++) {
        
        CommenModel *model = [CommenModel new];
        model.pic = picsArray[i];
        model.name = namesArray[i];
        model.centent = contentArray[i];
        model.period = periodArray[i];
        model.personNumber = personNumberArray[i];
        model.time = timesArray[i];
        model.level = levelsArray[i];
        
        [self.modelsArray addObject:model];
    }
}




#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NoStarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[NoStarTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.model = self.modelsArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CommenModel * model = self.modelsArray[indexPath.row];
    MoreViewController * moreVc = [[MoreViewController alloc] init];
    moreVc.name = model.name;
    [self.navigationController pushViewController:moreVc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (Screen_Width == 320 && Screen_Height == 480) {//苹果4
        
        return 130;
        
    }else if(Screen_Width == 320 && Screen_Height == 568){//苹果5
        
        return 130;
        
        
    }else if(Screen_Width == 375 && Screen_Height == 667){//苹果6
        
        return 150;
        
        
    }else{//苹果6puls及其以上
        
        return 160;
        
    }
}


- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    
    switch (peripheral.state) {
            
        case CBPeripheralManagerStatePoweredOn:
            
        {
            
            NSLog(@"蓝牙已连接");
        }
            
            break;
            
        default:
            
            [self closeAlertView];
            
            NSLog(@"蓝牙不可用");
            
            break;
            
    }
    
}

#pragma mark 私有方法

-(void)closeAlertView{
    
    //    if (isOpen == 0) {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"请打开蓝牙" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        [self openBlueTooth];
        
    }];
    
    // Add the actions.
    
    [alertController addAction:cancelAction];
    
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    //}
    
}


-(void)openBlueTooth{
    
    NSURL *url = [NSURL URLWithString:@"prefs:root=Bluetooth"];
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
        
    }
    
}

@end
